package com.along.textdemo.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import androidx.annotation.Nullable;

import com.along.textdemo.R;

/**
 * 音频波浪动画
 * 中间高，两边低的计算绘制波动高度，中间穿插 几个随机数量的高度
 */
public class LineLoadingView2 extends View {
    private final String TAG = "AudioLoadingView";
    private Paint paint;
    private RectF rectF;
    //属性
    private int lineSpace;  //两条线之间的宽度
    private float changeStep; //动画幅度
    private int lineCount;  //堆数，每堆 根线条
    private int lineWidth;  //线宽度
    private int changeSpeed;    //变化速度  次/秒
    private int color;  //颜色
    private int width;  //dp
    private int maxHeight;  //最高高度
    private int minHeight;  //最低高度
    private float radius;   //线条弧度

    //draw用到的中间参数
    private int tempHeight = 0; //

    private List<Integer> minList = new ArrayList<>();
    private Random mRandom = new Random();

    public LineLoadingView2(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LineLoadingView);
        init(typedArray);
    }

    //初始化配置，准备宽高数据
    private void init(TypedArray typedArray) {
        //自主控制线宽、线高、圆角、颜色、数量（每8个一堆）、动画幅度
        lineCount = typedArray.getInteger(R.styleable.LineLoadingView_lineCount, 1);//默认1
        lineWidth = (int) typedArray.getDimension(R.styleable.LineLoadingView_lineWidth, dip2px(3));    //默认3
        lineSpace = (int) typedArray.getDimension(R.styleable.LineLoadingView_lineSpace, dip2px(7));    //默认7
        color = typedArray.getColor(R.styleable.LineLoadingView_lineColor, Color.parseColor("#FFFFE105"));
        maxHeight = (int) typedArray.getDimension(R.styleable.LineLoadingView_lineMaxHeight, dip2px(50));
        minHeight = (int) typedArray.getDimension(R.styleable.LineLoadingView_lineMinHeight, dip2px(10));
        changeStep = typedArray.getFloat(R.styleable.LineLoadingView_changeStep, 0.1f) * maxHeight;    //默认0.1
        changeSpeed = typedArray.getInt(R.styleable.LineLoadingView_changeSpeed, dip2px(6));
        radius = typedArray.getDimension(R.styleable.LineLoadingView_lineRadius, dip2px(3));

        width = lineCount * 8 * (lineSpace + lineWidth) + lineWidth;
        tempHeight =minHeight;
        Log.i(TAG, "MusicLoadingView: " + lineCount
                + " 变化： " + changeStep
                + "  线宽： " + lineWidth
                + "  线空隙： " + lineSpace
                + "  高度：" + maxHeight
                + " 最低： " + minHeight
                + "  width:" + width);

        paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
        paint.setColor(color);
        rectF = new RectF();
        rectF.left = 0;
        rectF.right = lineWidth;

        for (int i = 0; i <= lineCount * 8; i++) {
                minList.add(i);
        }
    }

    public void stopAnim() {
        isGetVoice = false;
        tempHeight = 0;
        invalidate();
    }

    boolean isGetVoice = false;
    public void getVoiceValue(int value){
        isGetVoice =true;
        if (minHeight+value > maxHeight) {
            tempHeight = maxHeight;
        }else if (value <= 1){
            tempHeight = 0;
            isGetVoice =false;
        }else{
            tempHeight = value*2;
        }
        Log.i(TAG, "value: " + value
                        + " tempHeight： " + tempHeight);
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //设置宽高
        setMeasuredDimension(width, maxHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {  //第0秒
        super.onDraw(canvas);
        //5个一组

        /**
         * 这里能看出来其实是有规律的
         * 可以进一步封装,来自定义有多少种不同高度的线条
         * */
        if (isGetVoice){
            //这里添加一个4到8的随机数
            int num = mRandom.nextInt(8-4)+4;
//            for (int i : minList) {
//                if (i % num == 0) {
//                    draw(canvas, i, minHeight+tempHeight*3);
//                }else if (i % num == 3){
//                    draw(canvas, i, minHeight+tempHeight*2);
//                }else if (i % num == 2){
//                    draw(canvas, i, minHeight+tempHeight);
//                }else{
//                    draw(canvas, i, minHeight);
//                }
//            }
            //左半部分 绘制平滑的上升 柱状图
            for (int j = 0 ; j < minList.size()/2; j++){

                if (j % num == 0) {
                    draw(canvas, j, minHeight+tempHeight);
                }else if (j % num == 3) {
                    draw(canvas, j, minHeight + tempHeight * 2);
                }else {
                    //这里根据2次的平方，算声音的高度，在小于1时，平方的变化小，大于1时平方的变化大的特点
                    //获取的j 会除以10 再自身平方 来模拟，绘图两边低中间高的样式
                    //这里加1 再乘以10 是让这个 平方值的变化，以防由于太小被省区
                    double pp = (1 + Math.pow((double) j / 10, 2)) * 10;
                    Log.d(TAG, "   ----pp)===" + pp);
                    //所以这里得到的音量值，会被再除以10，来恢复变化
                    double ll = (tempHeight * pp) / 10;
                    Log.d(TAG, "   ----ll)===" + ll);

                    draw(canvas, j, minHeight + (int) ll);
                }
            }
            //右半部分 绘制平滑的下降 柱状图
            for (int k = minList.size()/2;k < minList.size(); k++){

                if (k % num == 0) {
                    draw(canvas, k, minHeight+tempHeight);
                }else if (k % num == 3) {
                    draw(canvas, k, minHeight + tempHeight * 2);
                }else {
                    //这里的计算思路同左侧，差别不过是 有大变小 从大于1到小于1
                    Log.d(TAG, "\nk==" + k);
                    double n = minList.size() - k;
                    double m = (1 + Math.pow(n / 10, 2)) * 10;
                    int s = (int) (tempHeight * m) / 10;

                    draw(canvas, k, minHeight + s);
                }
            }

        }else{
            for (int i : minList) {
            draw(canvas, i, minHeight);
            }
        }

    }

    //绘制，就是画一条竖线
    private void draw(Canvas canvas, int i,int temp) {  //i 标识位
        rectF.left = i * (lineWidth + lineSpace);
        rectF.right = rectF.left + lineWidth;
        int top = (maxHeight - temp) / 2;
        rectF.top = top;
        rectF.bottom = top + temp;
        canvas.drawRoundRect(rectF, radius, radius, paint);
    }



    public int dip2px(float dipValue) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }
}
